package com.example.xiazai;

import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.xiazai.bean.ProMsg;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import retrofit2.http.Url;

public class MyService extends Service {
    private String downloadUrl = "https://alissl.ucdl.pp.uc.cn/fs08/2019/07/05/1/110_17e4089aa3a4b819b08069681a9de74b.apk";
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onCreate() {
        super.onCreate();
        new Thread(){
            @Override
            public void run() {
                super.run();
                httpUrlConnection();
            }
        }.start();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void httpUrlConnection() {
        File file = new File("/storage/emulated/0/c.apk");
        if (!file.exists()){//如果文件不存在创建
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            URL url = new URL(downloadUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            int responseCode = urlConnection.getResponseCode();
            Log.i("111", "httpUrlConnection: "+responseCode);
            if (responseCode == 200){
                InputStream is = urlConnection.getInputStream();
                long max = urlConnection.getContentLengthLong();
                saveFile(file,max,is);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void saveFile(File file, long max, InputStream in) {
        EventBus.getDefault().post(new ProMsg(1,0,(int) max,0));
        try {
            FileOutputStream out = new FileOutputStream(file);
            int count = 0;//下载的进度
            int len = 0;//此次读取的长度
            byte[] buff = new byte[1024];
            while ((len = in.read(buff)) != -1) {//循环读取下载的文件信息
                out.write(buff, 0, len);
                count += len;
                //更新进度条,打印进度
                int n = (int) (((float)count/max)*100);
                Log.i("111", "saveFile: "+n+","+count);
                EventBus.getDefault().post(new ProMsg(2,count,(int)max,n));
            }
            out.close();
            in.close();
            Log.i("111", "saveFile: 下载完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
